#!/usr/bin/env python
# coding: utf-8

# In[1]:


exec(open('init_path.py').read())
exec((P_Lib/'GasStation.py').read_text())
get_ipython().run_line_magic('matplotlib', 'inline')


# ### Crude Oil Shocks

# In[2]:


oil_price_freq = '5min'


# In[10]:


f = P_GS_Data / 'Crude_data/CL_bcn9hle' / ('CL_continuous_adjusted_' + oil_price_freq + '.txt')
df = read_csv(f, names=['Date','Open','High','Low','Close','Volume'])
df['U'] = ((df.Close - df.Open)>0).astype(int)
df['dtYMDHM'] = to_datetime(df.Date)
df['dtYMD'] = df.dtYMDHM.dt.normalize()
df['dtYM'] = to_datetime(df.Date.str[:8] + '01')
df['Y'] = df.dtYM.dt.year
df.head(2)


# In[11]:


df_copy = df.copy()


# In[149]:


df = df_copy.copy()


# In[12]:


df = df[(df.Y>=2016) & (df.Y<=2018)]
df = df[df.U==0]
df['ChgPct'] = (df.Close - df.Open).abs() / df[['Open','Close']].mean(axis=1).values
df.head(2)


# In[13]:


ShockThld = df.groupby('dtYM').ChgPct.quantile(0.90); ShockThld.name = 'ShockThld'
df = df.merge(ShockThld, left_on='dtYM', right_index=True)
df.head(2)


# In[14]:


df['Shock'] = (df.ChgPct>=df.ShockThld).astype(int)
# df['Shock'] = (df.ChgPct>=df.ChgPct.quantile(.9)).astype(int)
df.head(2)


# In[15]:


#df.groupby('dtYM').Shock.sum().plot()


# In[8]:


t = df.dtYMDHM.dt
df['intYMD'] = t.year*10000 + t.month*100 + t.day
df['intHM'] = t.hour*100 + t.minute
df.head(2)


# In[9]:


# for shock_thld in ['90pctl','95pctl']:
shock_thld = 'P90'
f = P_GS_Data / 'Shock_Oil' / ('cruide_oil_' + oil_price_freq + '_drop_' + shock_thld + '.parquet')
print(f.stem)
df.loc[df.Shock==1, ['intYMD','intHM']].to_parquet(f, compression='brotli', index=False)


# In[156]:


# df.intYMD.unique()


# In[158]:


# df[df.intYMD==20160921]


# In[160]:


#df.groupby('intYMD').Shock.sum().sort_values().tail(2)
# .count().sort_values()


# In[ ]:





# In[ ]:




